package util

import (
	"fmt"
	"sort"
	"sync"
	"sync/atomic"
)

func CounterAddInt64(counters *sync.Map, key string, count int64) {
	val, _ := counters.LoadOrStore(key, new(int64))
	ptr := val.(*int64)
	atomic.AddInt64(ptr, count)
}

func CounterString(counters *sync.Map) (output string) {
	var results []string
	counters.Range(func(key, value interface{}) bool {
		k := key.(string)
		v := value.(*int64)
		results = append(results, fmt.Sprintf(" %s: %d\n", k, *v))
		return true
	})
	sort.Strings(results)
	for _, v := range results {
		output += v
	}

	return
}
